/****************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Torkild U. Resheim  - initial API and implementation
 *****************************************************************************/
package no.resheim.buildmonitor.ui.views.commands.log;

import no.resheim.buildmonitor.core.data.Job;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

/**
 * Command handler for comparing the current build with the previous build.
 * 
 * @author Torkild Ulvøy Resheim
 * @since 1.0
 */
public class CompareWithPrevious extends AbstractCompareHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		Object obj = getSelection(event);
		if (obj != null && obj instanceof Job) {
			Job job = (Job) obj;
			int left = job.getLastStartedBuildNumber();
			int right = job.getLastCompletedBuildNumber() - 1;
			if (right <= 0 || right == left) {
				MessageDialog.openInformation(Display.getCurrent()
						.getActiveShell(), "Cannot open compare editor",
						"There is no previous build to compare with.");
			} else if (left <= 0) {
				MessageDialog.openInformation(Display.getCurrent()
						.getActiveShell(), "Cannot open compare editor",
						"There is no last build build to compare with.");
			} else {
				compare(job, left, right);
			}
		}
		return null;
	}
}
